<%-- 
    Document   : Detail
    Created on : Dec 4, 2012, 10:39:25 AM
    Author     : Quang Thao
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%
    Bean.CartBean cart = null;
    if (session.getAttribute("Account") == null)    
        response.sendRedirect("../Account/LoginUser.jsp");
    else
    {
        if (session.getAttribute("cart") == null)
        {
            cart = new Bean.CartBean();
            Bean.AccountBean acc2 = (Bean.AccountBean) session.getAttribute("Account");
            cart.setAccount_ID(acc2.getID());
            session.setAttribute("cart",cart);
        }
        
        String isbn = null;
        
        if (request.getQueryString() != null)
        {
            String[] query = request.getQueryString().split("=");
            isbn = query[1];
        }
        
        if (isbn != null)
        {
            cart = (Bean.CartBean) session.getAttribute("cart");
            BO.BookBO bookBO = new BO.BookBO();
            Bean.BookBean book = new Bean.BookBean();
            book = bookBO.GetInfo(isbn);
            boolean check = true;
            for (int i =0;i<cart.getListBook().size();i++)
            {
                Bean.BookBean book2 = (Bean.BookBean) cart.getListBook().get(i);
                if (book.getISBN().equals(book2.getISBN()))
                {
                    book2.setQuantity(book2.getQuantity() + 1);
                    cart.getListBook().set(i, book2);
                    check = false;
                    break;
                }
            }
            if (check)
            {
                book.setQuantity(1);
                cart.getListBook().add(book);
            }
            session.setAttribute("cart", cart);
        }
            %>
            <html>
                <head>
                    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
                    <title>JSP Page</title>
                    <%@include file="../LayoutUser/InHead.jsp" %>
                    <script language="JavaScript">
                        function Update()
                        {
                            document.frmCartDetail.btnName.value = "Update";
                            frmCartDetail.submit();
                        }
                        function Delete()
                        {
                            document.frmCartDetail.btnName.value = "Delete";
                            frmCartDetail.submit();
                        }
                        function Pay()
                        {
                            document.frmCartDetail.btnName.value = "Pay";
                            frmCartDetail.submit();
                        }
                    </script>
                </head>
                <body>
                    <div id="wrapper">
                        <%@include file="../LayoutUser/Top.jsp" %>
                        <div id="middle">
                            <%@include file="../LayoutUser/Middle_Top.jsp" %>
                            <div class="left">
                                <div class="content">
                                    <h1>Thông tin giỏ hàng</h1>
                                    <form name="frmCartDetail" action="../../CartDetailServlet" method="POST">
                                    <table class="TableCart">
                                        <tr>
                                            <th>STT</th>
                                            <th>Tên sách</th>
                                            <th>Số lượng</th>
                                            <th>Giá tiền</th>
                                            <th>Tổng tiền</th>
                                            <th>Xóa</th>
                                        </tr>
                                        <%
                                            if (session.getAttribute("cart") != null)
                                            {
                                                cart = (Bean.CartBean) session.getAttribute("cart");
                                                for (int i=0;i<cart.getListBook().size();i++)
                                                {
                                                    Bean.BookBean book3 = (Bean.BookBean) cart.getListBook().get(i);
                                                    %>
                                                    
                                                        <tr>
                                                            <td>
                                                                <%= (i+1) %>
                                                            </td>
                                                            <td class="Left">
                                                                <%= book3.getName() %> 
                                                            </td>
                                                            <td>
                                                                <input name="<%= ("txt" + i )%>" 
                                                                       type="text" 
                                                                       value="<%= book3.getQuantity()%>"
                                                                       maxlength="3" 
                                                                       size="3"/>
                                                            </td>
                                                            <td>
                                                                <%= String.format("%,4.0f VNĐ",book3.getCost())%>
                                                            </td>
                                                            <td>
                                                                <%= String.format("%,4.0f VNĐ",(book3.getCost() * book3.getQuantity()))%>
                                                            </td>
                                                            <td>
                                                                <input name="<%= ("cbx" + i)%>" 
                                                                       type="checkbox" />
                                                            </td>
                                                        </tr>
                                                    <%
                                                }
                                            }
                                        %>
                                        <tr>
                                            <td colspan="2" class="Left">Tổng tiền cần thanh toán :</td>
                                            <td colspan="4" class="Right">
                                                <%= String.format("%,4.0f VNĐ",cart.getSumCost())%>
                                            </td>
                                        </tr>
                                        <tr>
                                        
                                            <td colspan="4" class="Left">
                                                <input type="hidden" value="" name="btnName"/>
                                                <input type="button" value="Cập nhật" name="btnUpdate" class="Button1"
                                                       onclick="Update()"/>
                                                &nbsp;
                                                <input type="button" value="Xóa tất cả" name="btnDelete" class="Button1"
                                                       onclick="Delete()"/>
                                                &nbsp;
                                                <input id="btnBack" type="button" value="Tiếp tục mua hàng" 
                                                onclick="javascript:history.back()"/>
                                            </td>
                                            <td colspan="2" class="Right">
                                                <input type="button" value="Thanh toán" name="btnPay" 
                                                       onclick="Pay()"/>
                                            </td>
                                        
                                        </tr>
                                    </table>
                                    </form>
                                </div>
                            </div>
                            <%@include file="../LayoutUser/Middle_Right.jsp" %>
                        </div>
                        <%@include file="../LayoutUser/Bottom.jsp" %>
                    </div>
                </body>
            </html>
        <%
    }
%>